<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue';

// 模拟消息数据
const notifications = ref([
  {
    id: 1,
    title: "退款申请已处理",
    content: "您的退款申请已通过审核，款项将在3个工作日内退回原支付账户",
    time: new Date(Date.now() - 3600000), // 1小时前
    read: false,
    type: "success"
  },
  {
    id: 2,
    title: "退货申请待处理",
    content: "您的退货申请已提交，请等待客服审核",
    time: new Date(Date.now() - 7200000), // 2小时前
    read: false,
    type: "warning"
  },
  {
    id: 3,
    title: "换货申请已拒绝",
    content: "由于商品已超出换货期限，您的换货申请已被拒绝",
    time: new Date(Date.now() - 10800000), // 3小时前
    read: true,
    type: "error"
  },
  {
    id: 4,
    title: "售后客服已联系您",
    content: "客服已通过电话联系您，请保持电话畅通",
    time: new Date(Date.now() - 21600000), // 6小时前
    read: true,
    type: "info"
  }
]);

// 计算未读消息数量
const unreadCount = computed(() => 
  notifications.value.filter(notification => !notification.read).length
);

// 格式化时间显示（不使用date-fns）
const formatTime = (date) => {
  const now = new Date();
  const diffInSeconds = Math.floor((now - date) / 1000);
  
  if (diffInSeconds < 60) {
    return `${diffInSeconds} 秒前`;
  } else if (diffInSeconds < 3600) {
    const minutes = Math.floor(diffInSeconds / 60);
    return `${minutes} 分钟前`;
  } else if (diffInSeconds < 86400) {
    const hours = Math.floor(diffInSeconds / 3600);
    return `${hours} 小时前`;
  } else if (diffInSeconds < 604800) {
    const days = Math.floor(diffInSeconds / 86400);
    return `${days} 天前`;
  } else {
    // 超过一周，显示完整日期
    return date.toLocaleDateString();
  }
};

// 获取类型标签文本
const getTypeLabel = (type) => {
  const labels = {
    success: '成功',
    warning: '警告',
    error: '错误',
    info: '信息'
  };
  return labels[type] || type;
};

// 标记消息为已读
const markAsRead = (id) => {
  const notification = notifications.value.find(n => n.id === id);
  if (notification) {
    notification.read = true;
  }
};

// 标记所有消息为已读
const markAllAsRead = () => {
  notifications.value.forEach(notification => {
    notification.read = true;
  });
};

// 模拟实时消息接收
const simulateNewNotification = () => {
  const types = ["success", "warning", "error", "info"];
  const titles = [
    "新的售后申请已创建",
    "售后处理进度更新",
    "客服已回复您的咨询",
    "售后政策更新通知"
  ];
  const contents = [
    "您的售后申请已创建，我们将尽快处理",
    "您的售后申请正在处理中，请耐心等待",
    "客服已回复您的咨询，请查看详情",
    "售后政策已更新，请查看最新条款"
  ];
  
  const newNotification = {
    id: notifications.value.length + 1,
    title: titles[Math.floor(Math.random() * titles.length)],
    content: contents[Math.floor(Math.random() * contents.length)],
    time: new Date(),
    read: false,
    type: types[Math.floor(Math.random() * types.length)]
  };
  
  notifications.value.unshift(newNotification);
};

// 定时接收新消息
let notificationInterval;
onMounted(() => {
  notificationInterval = setInterval(simulateNewNotification, 30000); // 每30秒模拟一条新消息
});

onUnmounted(() => {
  clearInterval(notificationInterval);
});
</script>

<template>
  <div class="notification-container">
    <div class="header">
      <h2>售后申请</h2>
      <div class="notification-badge" v-if="unreadCount > 0">
        {{ unreadCount }}
      </div>
      <button 
        class="mark-all-btn" 
        @click="markAllAsRead"
        :disabled="unreadCount === 0"
      >
        全部标为已读
      </button>
    </div>
    
    <div class="notification-list">
      <div 
        v-for="notification in notifications" 
        :key="notification.id"
        class="notification-item"
        :class="{ 'unread': !notification.read, 'read': notification.read }"
        @click="markAsRead(notification.id)"
      >
        <div class="notification-icon">
          <span :class="['icon', `icon-${notification.type}`]"></span>
        </div>
        <div class="notification-content">
          <div class="title">
            {{ notification.title }}
            <span v-if="!notification.read" class="new-indicator"></span>
          </div>
          <p class="content">{{ notification.content }}</p>
          <div class="meta">
            <span class="time">{{ formatTime(notification.time) }}</span>
            <span class="type">{{ getTypeLabel(notification.type) }}</span>
          </div>
        </div>
      </div>
      
      <div v-if="notifications.length === 0" class="empty-state">
        <p>暂无消息通知</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.notification-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

h2 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.notification-badge {
  background-color: #ff4d4f;
  color: white;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  margin-left: 10px;
}

.mark-all-btn {
  background-color: transparent;
  border: none;
  color: #1890ff;
  cursor: pointer;
  font-size: 14px;
  padding: 0;
  text-decoration: underline;
}

.mark-all-btn:disabled {
  color: #999;
  cursor: not-allowed;
}

.notification-list {
  max-height: 500px;
  overflow-y: auto;
  padding-right: 10px;
}

.notification-item {
  padding: 15px;
  margin-bottom: 15px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
}

.notification-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.notification-item.unread {
  background-color: #f5f7fa;
  border-left: 3px solid #1890ff;
}

.notification-item.read {
  background-color: #ffffff;
  border-left: 3px solid #e5e6eb;
}

.notification-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  flex-shrink: 0;
}

.icon {
  width: 20px;
  height: 20px;
  background-size: contain;
  background-repeat: no-repeat;
}

.icon-success {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%2352c41a'%3E%3Cpath d='M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17z'/%3E%3C/svg%3E");
}

.icon-warning {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23faad14'%3E%3Cpath d='M12 5.99L19.53 19H4.47L12 5.99M12 2L1 21h22L12 2zm1 14h-2v-2h2v2zm0-4h-2V7h2v5z'/%3E%3C/svg%3E");
}

.icon-error {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23ff4d4f'%3E%3Cpath d='M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z'/%3E%3C/svg%3E");
}

.icon-info {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%231890ff'%3E%3Cpath d='M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z'/%3E%3C/svg%3E");
}

.notification-content {
  flex: 1;
}

.title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
}

.new-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #1890ff;
  margin-left: 8px;
}

.content {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.meta {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #999;
}

.time {
  margin-right: 15px;
}

.type {
  padding: 2px 6px;
  border-radius: 3px;
  text-transform: capitalize;
}

.type.success {
  background-color: #f6ffed;
  color: #52c41a;
}

.type.warning {
  background-color: #fffbe6;
  color: #faad14;
}

.type.error {
  background-color: #fff2f0;
  color: #ff4d4f;
}

.type.info {
  background-color: #e6f7ff;
  color: #1890ff;
}

.empty-state {
  text-align: center;
  padding: 30px 0;
  color: #999;
}

/* 滚动条样式 */
.notification-list::-webkit-scrollbar {
  width: 6px;
}

.notification-list::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 10px;
}

.notification-list::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 10px;
}

.notification-list::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.notification-item {
  animation: fadeIn 0.3s ease forwards;
}

/* 消息类型颜色映射 */
.success { color: #52c41a; }
.warning { color: #faad14; }
.error { color: #ff4d4f; }
.info { color: #1890ff; }
</style>
